package com.insurance.service.impl;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.mapper.PublicCityMapper;
import com.insurance.service.PublicCityService;
import com.insurance.utils.ConversionDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/29 11:22
 * Description:
 * Version: V1.0
 */
@Service
public class PublicCityServiceImpl implements PublicCityService {

    @Autowired
    PublicCityMapper publicCityMapper;

    @Override
    public ResultData queryByCity(int province_id) throws SQLException {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.queryByCity(province_id);
        if (list.size()>0){
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryByProvinceDriver() throws SQLException{
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.queryByProvinceDriver();
        if (list.size()>0){
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryByAreaDriver(int cityId)throws SQLException {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.queryByAreaDriver(cityId);
        if (list.size()>0){
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryunreadInformByUserId(int user_id) {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.queryunreadInformByUserId(user_id);
        if (list.size()>0){
            for (Map map:list) {
                map.put("information_time", ConversionDateUtils.conversionDateStringUtils(map.get("information_time")));
            }
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData queryreadInformByUserId(int user_id) {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.queryreadInformByUserId(user_id);
        if (list.size()>0){
            for (Map map:list) {
                map.put("information_time", ConversionDateUtils.conversionDateStringUtils(map.get("information_time")));
            }
            resultData.setData(list);
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }

    @Override
    public ResultData readInformByUserId(int user_id) {
        ResultData resultData=new ResultData();
        int list=publicCityMapper.readInformByUserId(user_id);
        if (list>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查看消息成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查看消息失败");
        }
        return resultData;
    }

    @Override
    public ResultData verificationUserName(String userName) {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.verificationUserName();
        for (Map map:list) {
            if (userName.equals(map.get("user_name"))){
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("用户名不可用");
                return resultData;
            }
        }
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("用户名可用");
        return  resultData;
    }

    @Override
    public ResultData verificationUserPhone(String userPhone) {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.verificationUserName();
        for (Map map:list) {
            if (userPhone.equals(map.get("user_phone"))){
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("电话不可用");
                return resultData;
            }
        }
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("电话可用");
        return  resultData;
    }

    @Override
    public ResultData verificationUserEmail(String userEmail) {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.verificationUserName();
        for (Map map:list) {
            if (userEmail.equals(map.get("user_email"))){
                resultData.setCode(Contants.FAILURE);
                resultData.setMsg("邮箱不可用");
                return resultData;
            }
        }
        resultData.setCode(Contants.SUCCESS);
        resultData.setMsg("邮箱可用");
        return  resultData;
    }

    @Override
    public ResultData updateverificationUserName(Integer user_id) {
        ResultData resultData=new ResultData();
        List<Map> list= publicCityMapper.updateverificationUserName(user_id);
        if (list.size()>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("查无数据");
        }
        return resultData;
    }
    @Override
    public ResultData rankingDriver() {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.rankingDriver();
        if (list.size()>0){
                int j=1;
            for (Map map:list){
                map.put("num",j);
                j++;
            }
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setMsg("查无数据");
            resultData.setCode(Contants.FAILURE);
        }
        return resultData;
    }

    @Override
    public ResultData countOrder() {
        ResultData resultData=new ResultData();
        List<Map> list=publicCityMapper.countOrder();
        if (list.size()>0){
            resultData.setMsg("查询成功");
            resultData.setCode(Contants.SUCCESS);
            resultData.setData(list);
        }else {
            resultData.setMsg("查无数据");
            resultData.setCode(Contants.FAILURE);
        }
        return resultData;
    }

}
